{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0bb1cbb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7edd172f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "filename = '../data/nyc-parking-violations-2020.csv'\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['Plate ID', 'Registration State', 'Plate Type', 'Feet From Curb',\n",
    "                        'Vehicle Make', 'Vehicle Color'])\n",
    "df.columns = ['pid', 'state', 'ptype', 'make', 'color', 'feet']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed955dcd",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "In `df.query`, we can use the words `and` and `or`, rather than the symbols `&` and `|`, thanks to the `numexpr` library. Rewrite our final query using the words. Does this change the speed at all?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2a1b21d5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "914 ms ± 7.43 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df.query('state == \"NY\" and ptype == \"PAS\" and color == \"WHITE\" and feet > 1 and make == \"TOYOT\"')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33611a51",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "I prefer measuring distance in meters, rather than in feet. I thus want to find all of the cars that were ticketed when they were more than 1 meter from the curb. Perform this query using the traditional `df.loc` and also using `df.query`. Which one runs faster?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b95a0fc2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "63.2 ms ± 2.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df.loc[(df['feet'] * 0.3048) > 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d3a6a8c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "84.4 ms ± 1.51 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df.query('(feet * 0.3048) > 1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "261622c5",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "What if we modify our query, such that we look for cars that are > 1 meter from the curb and the state is New York? Which query runs faster, and by how much?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f51ddb07",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "507 ms ± 1.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df.loc[((df['feet'] * 0.3048) > 1) & (df['state'] == 'NY')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5ebb17b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "314 ms ± 4.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df.query('(feet * 0.3048) > 1 and state == \"NY\" ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ada77895",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
